package com.mdc.mansion.core.modules.order.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.jboss.logging.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mdc.mansion.common.util.StringUtils;
import com.mdc.mansion.core.modules.basedata.PushMessageService;
import com.mdc.mansion.core.modules.dishes.RestaurantDishesService;
import com.mdc.mansion.core.modules.order.KitchenDishesService;
import com.mdc.mansion.core.modules.order.RestaurantOrderComboItemService;
import com.mdc.mansion.core.modules.order.RestaurantOrderItemService;
import com.mdc.mansion.core.modules.order.RestaurantOrderService;
import com.mdc.mansion.core.modules.restaurant.RestaurantTableService;
import com.mdc.mansion.dataaccess.common.context.ContextHolder;
import com.mdc.mansion.dataaccess.common.persistence.Page;
import com.mdc.mansion.dataaccess.modules.basedata.entity.PushMessage;
import com.mdc.mansion.dataaccess.modules.dishes.entity.RestaurantDishes;
import com.mdc.mansion.dataaccess.modules.order.entity.RestaurantOrderComboItem;
import com.mdc.mansion.dataaccess.modules.order.entity.RestaurantOrderItem;
import com.mdc.mansion.dataaccess.modules.order.mo.RestaurantOderItemUnionMo;
import com.mdc.mansion.dataaccess.modules.order.mo.RestaurantOrderComboItemMO;
import com.mdc.mansion.dataaccess.modules.order.mo.RestaurantOrderItemMO;

/**<p>com.mdc.mansion.core.modules.dishes.impl.KitchenDishesServiceImpl</p>
 * <p>Title: KitchenDishesServiceImpl</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * @version 1.0
 */
@Service
public class KitchenDishesServiceImpl implements KitchenDishesService {
	
	Logger log = Logger.getLogger(KitchenDishesServiceImpl.class) ;

    @Resource
    private RestaurantDishesService restaurantDishesService;
    @Resource
    private RestaurantOrderComboItemService restaurantOrderComboItemService;
    @Resource
    private RestaurantOrderItemService restaurantOrderItemService;
    @Resource
    private RestaurantOrderService restaurantOrderService;
    @Resource
    private RestaurantTableService restaurantTableService;
    @Resource
    private PushMessageService pushMessageService;
    
    @Override
    @Transactional
    public void changeKitchenOfferFlag(String restaurantDishesId,String kitchenOfferFlag) {
        RestaurantDishes restaurantDishes = new RestaurantDishes();
        restaurantDishes.setId(restaurantDishesId);
  //      restaurantDishes.setStockoutFlag(kitchenOfferFlag);
        restaurantDishes.setStockoutFlag(Integer.parseInt(kitchenOfferFlag));
        restaurantDishesService.updateSelective(restaurantDishes);
        // 修改订单中未确认的菜品状态
        // 修改套餐中未确认的菜品状态
        //判断是缺货状态
        restaurantDishes =  restaurantDishesService.get(restaurantDishesId);
        if("1".equals(kitchenOfferFlag)){
            RestaurantOrderComboItemMO restaurantOrderComboItemMO = new RestaurantOrderComboItemMO();
            restaurantOrderComboItemMO.setDelFlag("0");
            restaurantOrderComboItemMO.setRestaurantDishesId(restaurantDishesId);
            restaurantOrderComboItemMO.setDishesStatus(4);
            restaurantOrderComboItemMO.setKitchenOutStock("1");// 厨房缺货
            restaurantOrderComboItemService.updateByParam(restaurantOrderComboItemMO);
                       
            RestaurantOrderItemMO restaurantOrderItemMO = new RestaurantOrderItemMO();
            restaurantOrderItemMO.setDelFlag("0");
            restaurantOrderItemMO.setRestaurantDishesId(restaurantDishesId);
            restaurantOrderItemMO.setDishesStatus(4);
            restaurantOrderItemMO.setKitchenOutStock("1");// 厨房缺货
            restaurantOrderItemService.updateByParam(restaurantOrderItemMO);

            if(null != restaurantDishes) {
                pushMessageService.pushBusinessMessage(PushMessage.BUSINESS_TYPE.STOCKOUT.getVal(),"",restaurantDishes.getChineseName());
            }
        }else{
            if(null != restaurantDishes) {
                pushMessageService.pushBusinessMessage(PushMessage.BUSINESS_TYPE.STOCKIN.getVal(),"",restaurantDishes.getChineseName());
            }
        }
      
    }
	
	/**
	 * 2018年7月16日 16:43:34 添加处理逻辑
	 * 状态只会从: 1-2 2-3 1-5
	 * 处理逻辑:
	 * 1、当目标状态是5时，如果有套餐ID则整个套餐菜品均取消(该套餐只要有一个产品进入制作则无法取消该套餐),无套餐菜品则只取消对应菜品
	 * 2、当目标状态为2、3时，如果套餐ID为空，则处理对应的菜品，套餐ID不为空，则优先处理子菜品状态,当子菜品状态均处理完成,则处理套餐ID的状态
	 * @param restaurantOrderId
	 * @param restaurantDishesId
	 * @param restaurantDishesComboId
	 * @param dishesStatus
	 * @param orderItemId
	 */
    @Transactional
    @Override
    public void changeStatus(String restaurantOrderId, String restaurantDishesId, String restaurantDishesComboId,String dishesStatus,String orderItemId) {
		String restaurantId = ContextHolder.currentRestaurantId();
		List<RestaurantOderItemUnionMo> orderItemList = null;
		String beforeStatus  = "1";
    	if(StringUtils.isNotBlank(orderItemId))
    	{
    		////如果combo为空则查询其是否为套餐或非套餐
			orderItemList = restaurantOrderItemService.useItemGetCombo(orderItemId);
			if(CollectionUtils.isNotEmpty(orderItemList))
			{
				
				/////更新异常,自己写SQL
				restaurantDishesComboId = orderItemList.get(0).getRestaurantDishesComboId();
				log.error(">>>>>>>>>>>>>>>>>>>>>>>>"+dishesStatus+"<<<<<<<<<<<<<<<<");
				///套餐数据
				if("2".equals(dishesStatus) || "3".equals(dishesStatus))
				{
					RestaurantOrderItemMO restaurantOrderItemMO = new RestaurantOrderItemMO();
					restaurantOrderItemMO.setRestaurantId(restaurantId);
					restaurantOrderItemMO.setDelFlag("0");
					if("2".equals(dishesStatus))
					{
						beforeStatus = "1";
					}else if("3".equals(dishesStatus))
					{
						beforeStatus = "2";
					}
					restaurantOrderItemMO.setBeforeStatus(beforeStatus);
					restaurantOrderItemMO.setDishesStatus(Integer.parseInt(dishesStatus));
					restaurantOrderItemMO.setRestaurantOrderId(restaurantOrderId);
					restaurantOrderItemMO.setRestaurantDishesComboId(restaurantDishesComboId);
					restaurantOrderItemService.updateItemByDetail(restaurantOrderItemMO);
				}
				
				////子菜品
				RestaurantOrderComboItemMO restaurantOrderComboItemMO = new RestaurantOrderComboItemMO();
				restaurantOrderComboItemMO.setDelFlag("0");
				restaurantOrderComboItemMO.setRestaurantId(restaurantId);
				restaurantOrderComboItemMO.setRestaurantOrderId(restaurantOrderId);
				restaurantOrderComboItemMO.setRestaurantDishesComboId(restaurantDishesComboId);
				restaurantOrderComboItemMO.setRestaurantDishesId(restaurantDishesId);
				restaurantOrderComboItemMO.setDishesStatus(Integer.parseInt(dishesStatus));
				restaurantOrderComboItemMO.setId(orderItemId);
				restaurantOrderComboItemService.updateSelective(restaurantOrderComboItemMO);
			}else {
//				restaurantDishesComboId = "";
				// 普通菜品
				RestaurantOrderItemMO restaurantOrderItemMO = new RestaurantOrderItemMO();
				restaurantOrderItemMO.setRestaurantId(restaurantId);
				restaurantOrderItemMO.setDelFlag("0");
				restaurantOrderItemMO.setRestaurantOrderId(restaurantOrderId);
				restaurantOrderItemMO.setRestaurantDishesId(restaurantDishesId);
				restaurantOrderItemMO.setDishesStatus(Integer.parseInt(dishesStatus));
				restaurantOrderItemMO.setId(orderItemId);
				restaurantOrderItemService.updateSelective(restaurantOrderItemMO);
			}
			
		}
		
    }

    /**
     * 获取厨房订单菜品列表
     * (non-Javadoc)
    * @param status
    * @return
    * @see com.mdc.mansion.core.modules.order.KitchenDishesService#dishesList(java.lang.String)
     */
    @Override
    public List<RestaurantOrderItem> dishesList(String status) {
        RestaurantOrderItem restaurantOrderItem = new RestaurantOrderItem();
        restaurantOrderItem.setRestaurantId(ContextHolder.currentRestaurantId());
//        restaurantOrderItem.setRestaurantId("53392e725e3942cbb7080f81e31b8e0d");
        restaurantOrderItem.setDishesStatus(Integer.parseInt(status));
        Page<RestaurantOrderItem> page = new Page<RestaurantOrderItem>(-1);
        restaurantOrderItem.setPage(page);
        return restaurantOrderItemService.findList(restaurantOrderItem);
    }

    /**
     * 获取厨房订单套餐列表
     * (non-Javadoc)
    * @param status
    * @return
    * @see com.mdc.mansion.core.modules.order.KitchenDishesService#dishesComboList(java.lang.String)
     */
    @Override
    public List<RestaurantOrderComboItem> dishesComboList(String status) {
        RestaurantOrderComboItem restaurantOrderComboItem = new RestaurantOrderComboItem();
        restaurantOrderComboItem.setRestaurantId(ContextHolder.currentRestaurantId());
//        restaurantOrderComboItem.setRestaurantId("53392e725e3942cbb7080f81e31b8e0d");
        restaurantOrderComboItem.setDishesStatus(Integer.parseInt(status));
        Page<RestaurantOrderComboItem> page = new Page<RestaurantOrderComboItem>(-1);
        restaurantOrderComboItem.setPage(page);
        return restaurantOrderComboItemService.findList(restaurantOrderComboItem);
    }

	@Override
	public List<RestaurantOrderItemMO> findKitchenDishesListByStatus(Map<String, String> paraMap) {
		return restaurantOrderItemService.findKitchenDishesListByStatus(paraMap);
	}

	@Override
	public List<RestaurantOderItemUnionMo> findKitchenDishesList(Map<String, String> paraMap) {
		return restaurantOrderItemService.findKitchenDishesList(paraMap);
	}

	@Override
	public List<RestaurantOrderItem> getDishesItemList(RestaurantOrderItem item) {
		return restaurantOrderItemService.findList(item);
	}

	@Override
	public List<String> findKitchenItemList(RestaurantOrderItem item) {
		return restaurantOrderItemService.findKitchenItemList(item);
	}

}
